<!-- 推广升级配置 -->
<template>
    <div>
        <div class="table-page-search-wrapper">
            <a-form layout="inline">
                <a-row type="flex" justify="space-between" :gutter="48">
                    <a-col :md="16" :sm="24">
                    </a-col>
                    <a-col :md="4">
                        <a-form-item >
                            <a-button style="margin-right: 20px" type="primary" icon="plus" @click="add()">新增</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>
        <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ pageSize: 10, total: total ,showSizeChanger: true,pageSize: size}" size="small">
            <div slot="grade" slot-scope="grade">
                <div v-for="(item,i) in gradeList" :key="item.id + 'i'">
                    <a-tag v-if="item.grade == grade" :color="colors[i%13]">{{item.gradeName}}</a-tag>
                </div>
            </div>
            <div slot="directNumber" slot-scope="directNumber">
                <div>{{directNumber}}人</div>
            </div>
            <div slot="directGrade" slot-scope="directGrade">
                <div v-for="(item,i) in gradeList" :key="item.id + 'i'">
                    <a-tag v-if="item.grade == directGrade" :color="colors[i%13]">{{item.gradeName}}</a-tag>
                </div>
            </div>
            <div slot="teamNumber" slot-scope="teamNumber">
                <div>{{teamNumber}}人</div>
            </div>
            <div slot="buyType" slot-scope="buyType">
                <div v-for="(item,i) in typeList" :key="item.id + 'i'">
                    <a-tag v-if="item.id == buyType" :color="colors[i%13]">{{item.name}}</a-tag>
                </div>
            </div>
            <div slot="topUpType" slot-scope="topUpType">
                <div v-for="(item,i) in typeList" :key="item.id + 'i'">
                    <a-tag v-if="item.id == topUpType" :color="colors[i%13]">{{item.name}}</a-tag>
                </div>
            </div>
             <div slot="isOn" slot-scope="isOn">
                <a-switch checked-children="开" un-checked-children="关" disabled :checked="isOn == 1 ? true : false" />
            </div>
            <template slot="operation" slot-scope="text, record">
                <div class="editable-row-operations">
                    <a-button type="link" @click="update(record)">修改</a-button>
                    <a-popconfirm
                        title="是否确认删除当前配置?"
                        ok-text="Yes"
                        cancel-text="No"
                        @confirm="del(record)"
                    >
                        <a-button type="link" class="errorColor">删除</a-button>
                    </a-popconfirm>
                </div>
            </template>
        </a-table>

        <!-- 添加、修改推广升级配置 -->
        <a-modal
            title="添加、修改等级升级配置"
            :visible="visible"
            :confirm-loading="confirmLoading"
            @ok="handleOk"
            @cancel="visible = false"
            >
            <div>
                <a-form :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }">
                    <a-card title="条件一" :bordered="false">
                        <a-form-item label="升级等级">
                            <a-select v-model="item.grade" placeholder="请选择" allowClear>
                                <a-select-option v-for="(item,i) in gradeList" :value="item.grade" :key="i">{{item.gradeName}}</a-select-option>
                            </a-select>
                        </a-form-item>
                        <a-form-item label="直推人数">
                            <a-input v-model="item.directNumber" placeholder="请输入直推人数" allow-clear></a-input>
                        </a-form-item>
                        <a-form-item label="直推等级">
                            <a-select v-model="item.directGrade" placeholder="请选择" allowClear>
                                <a-select-option v-for="(item,i) in gradeList" :value="item.grade" :key="i">{{item.gradeName}}</a-select-option>
                            </a-select>
                        </a-form-item>
                        <a-form-item label="团队人数">
                            <a-input v-model="item.teamNumber" placeholder="请输入团队人数" allow-clear></a-input>
                        </a-form-item>
                        <a-form-item label="下单金额">
                            <a-input v-model="item.buyAmount" placeholder="请输入下单金额" allow-clear></a-input>
                        </a-form-item>
                        <a-form-item label="下单人员">
                            <a-select v-model="item.buyType" placeholder="请选择" allowClear>
                                <a-select-option v-for="(item,i) in typeList" :value="item.id" :key="i">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-card>
                    <a-card title="条件二" :bordered="false">
                        <a-form-item label="充值金额">
                            <a-input v-model="item.topUpAmount" placeholder="请输入充值金额" allow-clear></a-input>
                        </a-form-item>
                        <a-form-item label="充值人员">
                            <a-select v-model="item.topUpType" placeholder="请选择" allowClear>
                                <a-select-option v-for="(item,i) in typeList" :value="item.id" :key="i">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-card>
                     <a-form-item label="是否开启">
                        <a-radio-group v-model="item.isOn" >
                            <a-radio :value="0">关闭</a-radio>
                            <a-radio :value="1">开启</a-radio>
                        </a-radio-group>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { queryGradeUp , updateGradeUp , queryGradeProduct } from '@/api/product';
import { columns } from './table';
export default {
    data () {
        return {
            columns,
            brandId: this.global.brandId,
            brandList: [],
            dataList: [],
            isload: false,
            page: 1,
            size: 10,
            total: 0,
            grade: undefined,
            visible: false,
            confirmLoading: false,
            item: {
                id: '', 
                grade: '', // 等级
                directNumber: '', // 直推人数
                directGrade: undefined, // 直推等级
                teamNumber: '', // 团队人数
                buyAmount: '', // 下单金额
                buyType: undefined, // 下单人员
                topUpAmount: '', // 充值金额
                topUpType: undefined, // 充值人员
                isOn: '1', // 是否开启
            },
            typeList: [
                { id: 0, name: '本人'},
                { id: 1, name: '直推'},
                { id: 2, name: '团队'},
            ],
            statusList: [
                { id: 1, name: '开启'},
                { id: 0, name: '关闭'},
            ],
            colors: ['blue','purple','cyan','green','orange','red','pink','brown','indigo','lime','purple','teal','yellow','amber'],
            gradeList: [],
        };
    },
    async created(){
        try {
            await this.getBrandGrade();
            this.getConfig()
        } catch (error) {
            this.$notification['error']({
                message: '提示',
                description: '网路丢失，请稍后再试！',
                duration: 8,
            })
        }
    },
    methods: {
        // 获取品牌等级产品
        getBrandGrade(){
            let _this = this
            return new Promise((resolve,reject) =>{
                queryGradeProduct({'brandId': _this.brandId,'page': 0,'size': 99})
                .then(res=>{
                    if(res.success){
                        _this.gradeList = res.data.content;
                        resolve();
                    }else{
                        reject(res.errMessage)
                    }
                })
            })
        },
        // 获取推广升级配置
        getConfig(){
            let _this = this
            console.log(_this.brandId,'brandid')
            const params = {
                'brandId':_this.brandId,
                'grade':_this.grade,
                'page':_this.page - 1,
                'size':_this.size,
            }
            queryGradeUp(params)
            .then(res=>{
                if(res.success){
                    _this.dataList = res.data.content
                    _this.total = res.data.totalElements
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getGrade()
        },
        // 添加产品
        add(){
            if(!this.brandId){
                this.$message.warning('请先选择品牌')
                return
            }
            this.item.id = ''
            this.item.brandId = this.brandId
            this.item.grade = undefined
            this.item.directNumber = ''
            this.item.directGrade = undefined
            this.item.teamNumber = ''
            this.item.buyAmount = ''
            this.item.buyType = undefined
            this.item.topUpAmount = ''
            this.item.topUpType = undefined
            this.item.isOn = 1
            this.visible = true
        },
        // 修改产品
        update(item){
            this.item = JSON.parse(JSON.stringify(item))
            this.visible = true
        },
        // 确认添加、修改等级产品
        handleOk(){ 
            let _this = this
            const params = {
                'brandId':_this.item.brandId,
                'id':_this.item.id, 
                'grade':_this.item.grade,
                'directNumber':_this.item.directNumber,
                'directGrade':_this.item.directGrade,
                'teamNumber':_this.item.teamNumber,
                'buyAmount':_this.item.buyAmount,
                'buyType':_this.item.buyType,
                'topUpAmount':_this.item.topUpAmount,
                'topUpType':_this.item.topUpType,
                'isOn':_this.item.isOn,
            }
            updateGradeUp(params)
            .then(res=>{
                if(res.success){
                    _this.visible = false
                    _this.getConfig()
                    _this.$notification['success']({
                        message: '提示',
                        description: _this.item.id ? '修改成功' : '添加成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 删除产品
        del(item){
            let _this = this
            const params = {
                'brandId':_this.brandId,
                'id':item.id,
                'grade':item.grade,
                'directNumber':item.directNumber,
                'directGrade':item.directGrade,
                'teamNumber':item.teamNumber,
                'buyAmount':item.buyAmount,
                'buyType':item.buyType,
                'topUpAmount':item.topUpAmount,
                'topUpType':item.topUpType,
                'isDelete':1,
            }
            updateGradeUp(params)
            .then(res=>{
                if(res.success){
                    _this.visible = false
                    _this.getConfig()
                    _this.$notification['success']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
    }
}

</script>
<style scoped>

</style>